$(document)
		.ready(
				function() {

					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var detailsList = new Array();
					var comfirmDetails = new Array();
					var pageIndex = $("#pageIndex").val();
					var isScanCode = true;
					var pageCount = $("#pageCount").val();
					var counter = 1; // 读取信息计数器
					var isReading = false; // 是否处于监视用户输入状态
					var currentInfo = "";
					var len = 0;// 标记数组的长度，删除时-1
					var itemPSize = 5;
					var itemPageIndex = 1;
					var itemPageCount = 1;
					var itemTotalCount = 0;
					var rowNum = 0;
					var confirmRowNum = 0;
					var isView = false;
					var ispass = -1;

					$("#proInforSearchItem")
							.delegate(
									$(this),
									"click",
									function() {

										var proSnCode = $("#proSnCode").val();
										var typeId = $("#reType").val();

										if (proSnCode != null
												&& proSnCode != ""
												&& proSnCode != "undefined") {
											var queryUrl = basePath
													+ "/manage/stocksDetail/queryStockDetailByCode.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															snCode : proSnCode,
															reType : typeId
														},
														cache : false,

														success : function(data) {
															// addProductConfirm(data)

															if (typeId == 1) {
																addProductConfirm(data);
															}
															if (typeId == 2) {
																addGiftConfirm(data);
															}
															return;
														},
														error : function(html) {
															alert("load Data failly");
															return;
														}
													});
										}
									})

					$("#proInforTable")
							.delegate(
									"button[name='delConfirmItem']",
									'click',
									function() {
										if (confirm("是否要删除该记录?") == true) {
											var trId = $(this).attr("objId");
											var id = $(this).parent().parent()
													.attr("id");
											// alert("trId=" + trId)

											if (trId == null && trId == ""
													&& trId == "undefined") {
												comfirmDetails.splice(trId, 1);
												$(this).parent().parent()
														.remove();
												len = comfirmDetails.length;
												getConfirmQuery(len);

											} else {

												var delUrl = "delItemConfirmById.do";
												$
														.ajax({
															url : delUrl,
															dataType : 'text',
															data : {
																id : id
															},
															cache : false,

															success : function(
																	data) {
																alert("delete Data successfully");
																comfirmDetails
																		.splice(
																				trId,
																				1);
																clearItemTable("proInforTable");
																len = comfirmDetails.length;
																getConfirmQuery(len);
																return;
															},
															error : function(
																	html) {
																alert("delete Data failly");
																return;
															}
														});

											}

										}

									});

					$("#giftInforTable")
							.delegate(
									"button[name='delConfirmItem']",
									'click',
									function() {
										if (confirm("是否要删除该记录?") == true) {
											var trId = $(this).attr("objId");
											var id = $(this).parent().parent()
													.attr("id");
											// alert("trId=" + trId)

											if (trId == null && trId == ""
													&& trId == "undefined") {
												comfirmDetails.splice(trId, 1);
												$(this).parent().parent()
														.remove();
												getConfirmQuery(comfirmDetails.length);

											} else {

												var delUrl = "delItemConfirmById.do";
												$
														.ajax({
															url : delUrl,
															dataType : 'text',
															data : {
																id : id
															},
															cache : false,

															success : function(
																	data) {
																if (data == "success") {
																	alert("delete Data successfully");
																	comfirmDetails
																			.splice(
																					trId,
																					1);
																	clearItemTable("giftInforTable");

																	getConfirmQuery(comfirmDetails.length);
																	return;
																} else {
																	alert("delete Data failly");
																	return;
																}

															},
															error : function(
																	html) {
																alert("delete Data failly");
																return;
															}
														});

											}

										}

									});

					function addProductConfirm(data) {

						comfirmDetails.push(data);

						getConfirmQuery(comfirmDetails.length);

					}

					function getConfirmQuery(len) {
						/**
						 * 添加的内分页
						 */
						// alert("len-------------" + len);
						itemTotalCount = len;

						if (len <= itemPSize) {
							addPageCount = 1;
						} else {
							if (len % itemPSize == 0) {
								addPageCount = len / itemPSize;
							} else {
								addPageCount = Math.floor((len / itemPSize)) + 1;
							}

						}
						// alert("itemPageCount---------" + addPageCount);
						$('#confirmPagination')
								.jqPaginator(
										{
											totalPages : addPageCount,
											visiblePages : addPageCount,
											currentPage : 1,
											prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
											page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
											next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
											onPageChange : function(num, type) {

												var itemTable;
												var reType = $("#reType").val();
												// alert("reType="+reType);
												if (reType == 1) {
													clearItemTable("proInforTable");
													itemTable = $("#proInforTable");
												}
												if (reType == 2) {
													clearItemTable("giftInforTable");
													itemTable = $("#giftInforTable");
												}
												// alert("itemTable="+itemTable)
												var start = num * itemPSize
														- itemPSize;
												var end = num * itemPSize;
												// rowNum++;
												var proId = $("#proId").val();
												// alert("proId-------------"+
												// proId);
												var idx = 0;
												// alert("start---------" +
												// start);
												for (i = start; i < comfirmDetails.length; i++) {
													if (idx < 5) {

														addProductConfirmContent(
																itemTable,
																comfirmDetails[i],
																i);

														idx++;
													}
												}
											}
										});
						return true;
					}

					function addProductConfirmContent(itemTable, data,
							confirmRowNum) {

						var operation = $("#operation").val();

						if (operation == "confirm") {
							var rowContent = "<tr id="
									+ confirmRowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ confirmRowNum
									+ "'  name='confirmDetails["
									+ confirmRowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode
									+ "' readonly='readonly' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' readonly='readonly'/>"
									+ "<input id='id_" + confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].id' itemName='id' value='" + data.id
									+ "' />" + "<input id='proName_" + data.id
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "' readonly='readonly'/>"
									+ "<input id='ruleName_" + confirmRowNum
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='snCode_"
									+ confirmRowNum
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].snCode' itemName='snCode' value='"
									+ data.snCode
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' id='salePrice_"
									+ confirmRowNum
									+ "' readonly='readonly'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].note' itemName='note' value='"
									+ data.note + "' id='note_" + confirmRowNum
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<button  class='btn btn-sm btn-danger' objId='"
									+ confirmRowNum
									+ "'  name='delConfirmItem' type='button'>删除</button>"
									+ "</td>";

							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
						} else {
							var rowContent = "<tr id="
									+ confirmRowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ confirmRowNum
									+ "'  name='confirmDetails["
									+ confirmRowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode
									+ "' readonly='readonly' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' />" + "<input id='id_"
									+ confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].id' itemName='id' value='" + data.id
									+ "' />" + "<input id='proName_" + data.id
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ confirmRowNum
									+ "' type='hidden' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "'/>"
									+ "<input id='ruleName_" + confirmRowNum
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='snCode_"
									+ confirmRowNum
									+ "' type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].snCode' itemName='snCode' value='"
									+ data.snCode
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' id='salePrice_"
									+ confirmRowNum
									+ "' readonly='readonly'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input type='text' name='confirmDetails["
									+ confirmRowNum
									+ "].note' itemName='note' value='"
									+ data.note + "' id='note_" + confirmRowNum
									+ "' readonly='readonly'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<button  class='btn btn-sm btn-danger' objId='"
									+ confirmRowNum
									+ "'  name='delConfirmItem' type='button' disabled='disabled'>删除</button>"
									+ "</td>";

							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
						}

					}

					function addGiftConfirm(data) {
						comfirmDetails.push(data);
						getConfirmQuery(comfirmDetails.length);
					}

					$("#giftSearchItem")
							.delegate(
									$(this),
									"click",
									function() {

										var giftSnCode = $("#giftSnCode").val();
										var typeId = $("#reType").val();
										if (giftSnCode != null
												&& giftSnCode != ""
												&& giftSnCode != "undefined") {

											// giftSearchItem 查找礼品

											if (giftSnCode != null
													&& giftSnCode != ""
													&& giftSnCode != "undefined") {
												var queryUrl = basePath
														+ "/manage/stocksDetail/queryStockDetailByCode.do";
												$
														.ajax({
															url : queryUrl,
															dataType : 'json',
															data : {
																snCode : giftSnCode,
																reType : typeId
															},
															cache : false,

															success : function(
																	data) {
																// addProductConfirm(data)

																if (typeId == 1) {
																	addProductConfirm(data);
																}
																if (typeId == 2) {
																	addGiftConfirm(data);
																}
																return;
															},
															error : function(
																	html) {
																alert("load Data failly");
																return;
															}
														});
											}

										}
									})

					$("#giftItemTable")
							.delegate(
									"input[itemName='total']",
									'blur',
									function() {
										var total = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var action = $("#action").val();
										var retype = $("#reType").val();
										var itemId = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='ruleId']")
												.val();
										var ruleName = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='ruleName']")
												.val();
										var proName = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='proName']")
												.val();
										var salePrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='salePrice']")
												.val();
										var enterPrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='enterPrice']")
												.val();
										if (total != null && total != ""
												&& total != "undefined") {
											if (total < 0) {
												alert("数量不能为负数。");
												$(this).val("");
												return;
											}
											if (parseInt(total) != total) {
												alert("请输入整数");
												$(this).val("");
												return;
											}
											var trId = $(this).parent()
													.parent().attr("id");
											var rowId = $(this).attr("rowId");
											// 库存检测

											if (action == 1 || action == 3) {
												checkStock(retype, action,
														itemId, total, trId,
														salePrice, enterPrice,
														proName, ruleName,
														rowId);

												var ele = detailsList[rowId];

												ele.total = total;
												ele.salePrice = salePrice;
												ele.enterPrice = enterPrice;
												if (action == 0 || action == 2
														|| action == 3) {
													// alert("5555555555action="
													// + action);
													ele.summery = enterPrice
															* total;
												}
												if (action == 1) {
													// alert("44444444action="
													// + action);
													ele.summery = salePrice
															* total;

												}

												detailsList.splice(rowId, 1,
														ele);
												$("#summery_" + trId).val(
														ele.summery);
											} else {

												var ele = detailsList[rowId];

												ele.total = total;
												ele.salePrice = salePrice;
												ele.enterPrice = enterPrice;
												if (action == 0 || action == 2
														|| action == 3) {
													// alert("5555555555action="
													// + action);
													ele.summery = enterPrice
															* total;
												}
												if (action == 1) {
													// alert("44444444action="
													// + action);
													ele.summery = salePrice
															* total;
												}
												// alert("------------ele.summery>>>>>>>>>"+ele.summery);

												detailsList.splice(rowId, 1,
														ele);
												$("#summery_" + trId).val(
														ele.summery);
												return;
											}
										} else {
											$("#summery_" + trId).val("");
											$(this).val("");
											return;
										}

									});

					$("#itemsTable")
							.delegate(
									"input[itemName='total']",
									'blur',
									function() {

										var total = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var action = $("#action").val();
										var retype = $("#reType").val();
										var itemId = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='ruleId']")
												.val();
										var ruleName = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='ruleName']")
												.val();
										var proName = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='proName']")
												.val();
										var salePrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='salePrice']")
												.val();
										var enterPrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='enterPrice']")
												.val();

										if (total != null && total != ""
												&& total != "undefined") {
											if (total < 0) {
												alert("数量不能为负数。");
												$(this).val("");
												return;
											}
											if (parseInt(total) != total) {
												alert("请输入整数");
												$(this).val("");
												return;
											}
											var trId = $(this).parent()
													.parent().attr("id");
											var rowId = $(this).attr("rowId");
											// 库存检测

											if (action == 1 || action == 3) {
												checkStock(retype, action,
														itemId, total, trId,
														salePrice, enterPrice,
														proName, ruleName,
														rowId);

												var ele = detailsList[rowId];

												ele.total = total;
												ele.salePrice = salePrice;
												ele.enterPrice = enterPrice;
												if (action == 0 || action == 2
														|| action == 3) {
													// alert("5555555555action="
													// + action);
													ele.summery = enterPrice
															* total;
												}
												if (action == 1) {
													// alert("44444444action="
													// + action);
													ele.summery = salePrice
															* total;

												}

												detailsList.splice(rowId, 1,
														ele);
												$("#summery_" + trId).val(
														ele.summery);
											} else {

												var ele = detailsList[rowId];

												ele.total = total;
												ele.salePrice = salePrice;
												ele.enterPrice = enterPrice;
												if (action == 0 || action == 2
														|| action == 3) {
													// alert("5555555555action="
													// + action);
													ele.summery = enterPrice
															* total;
												}
												if (action == 1) {
													// alert("44444444action="
													// + action);
													ele.summery = salePrice
															* total;
												}

												detailsList.splice(rowId, 1,
														ele);
												$("#summery_" + trId).val(
														ele.summery);
												return;
											}
										} else {
											$("#summery_" + trId).val("");
											$(this).val("");
											return;
										}
									});

					/**
					 * 库存检测
					 */
					function checkStock(retype, actId, itemId, total, trId,
							salePrice, enterPrice, proName, ruleName, rowId) {

						var ajaxUrl = "checkStock.do";
						$.ajax({
							url : ajaxUrl,
							data : {
								reType : retype,
								action : actId,
								ruleId : itemId,
								total : total
							},
							dataType : "text",
							success : function(data) {

								ispass = data;
								// alert(data);
								if (ispass == "fail") {

									alert("产品:" + proName + "规格：" + ruleName
											+ " 出库数量比库存量大，无法出库。");
									$("#total_" + trId).val("");
									$("#summery_" + trId).val("");
									return;
								}

								var ele = detailsList[rowId];

								ele.total = total;
								ele.salePrice = salePrice;
								ele.enterPrice = enterPrice;
								if (action == 0 || action == 2 || action == 3) {
									ele.summery = enterPrice * total;
								}
								if (action == 1) {
									ele.summery = salePrice * total;
								}
								detailsList.splice(rowId, 1, ele);
								$("#summery_" + trId).val(ele.summery);

							},
							error : function(data) {
								alert("加载数据出错");
								ispass = "fail";
								return;
							}
						});
					}

					$("#endTime").delegate($(this), 'click', function() {
						laydate({
							istime : false,
							format : 'YYYY-MM-DD'
						});
					});

					$("#startTime").delegate($(this), 'click', function() {
						laydate({
							istime : false,
							format : 'YYYY-MM-DD'
						});

					});

					$("#itemsTable")
							.delegate(
									"input[itemName='note']",
									'change',
									function() {
										var note = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										var ele = detailsList[rowId];
										ele.note = note;

										detailsList.splice(rowId, 1, ele);
									});

					$("#itemsTable")
							.delegate(
									"input[itemName='note']",
									'blur',
									function() {
										var note = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										var ele = detailsList[rowId];
										ele.note = note;

										detailsList.splice(rowId, 1, ele);
									});

					$("#giftItemTable")
							.delegate(
									"input[itemName='note']",
									'change',
									function() {
										var note = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										var ele = detailsList[rowId];
										ele.note = note;

										detailsList.splice(rowId, 1, ele);
									});

					$("#giftItemTable")
							.delegate(
									"input[itemName='note']",
									'blur',
									function() {
										var note = $(this).val();
										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										var ele = detailsList[rowId];
										ele.note = note;

										detailsList.splice(rowId, 1, ele);
									});

					$("#baseInfor")
							.delegate(
									"#reTypes",
									'change',
									function() {
										var retypeId = $(this).val()
										var stockId = $("#proId").val();
										var actionId = $("#actions").val();

										if (retypeId == null
												|| retypeId == "undefined") {
											//
											retypeId = 2;
										}
										$("#reType").val(retypeId);
										var operation = $("#operation").val();
										changeComponentsVisiable(stockId,
												retypeId, actionId, operation);
										clearArrAll();
										// 清空确认信息
										clearConfirmDetails();
									});

					$("#baseInfor")
							.delegate(
									"#actions",
									'change',
									function() {
										var actionId = $(this).val()
										var stockId = $("#proId").val();
										if (actionId == null
												|| actionId == "undefined") {
											alert("库单类型不能为空");
											return;
										}
										$("#action").val(actionId);
										var operation = $("#operation").val();
										var retypeId = $("#reType").val();
										changeComponentsVisiable(stockId,
												retypeId, actionId, operation);
										clearArrAll();
										// 清空确认信息
										clearConfirmDetails();

									});

					function changeComponentsVisiable(stockId, retypeId,
							actionId, operation) {

						// alert("operation------------>" + operation);
						var stockAction = actionId;
						var stockRetype = retypeId;
						if (operation == "view") {

							$("#saveBtn").hide();
							$("#confirmBtn").hide();
							$("#baseTab").addClass("active");
							$("#baseInfor").addClass("active");
							$("#itemsTab").removeClass("active");
							$("#itemList").removeClass("active");
							$("#confirmItems").removeClass("active");
							$("#confirmItem").removeClass("active");
							if (stockAction != 0) {
								$("#confirmItem").css('display', 'block');
							} else {
								$("#confirmItem").css('display', 'none');
							}
							if (stockRetype == 1) {

								$("#itemsTable").css('display', 'block');
								$("#giftItemTable").css('display', 'none');
								$("#proSearchInfor").css('display', 'block');
								$("#giftSearchInfor").css('display', 'none');
								$("#proConfirmInfor").css('display', 'block');
								$("#giftConfirmInfor").css('display', 'none');
								$("#proInforTable").css('display', 'block');
								$("#giftInforTable").css('display', 'none');

							}
							if (stockRetype == 2) {

								$("#itemsTable").css('display', 'none');
								$("#giftItemTable").css('display', 'block');
								$("#proSearchInfor").css('display', 'none');
								$("#giftSearchInfor").css('display', 'block');
								$("#proConfirmInfor").css('display', 'none');
								$("#giftConfirmInfor").css('display', 'block');
								$("#proInforTable").css('display', 'none');
								$("#giftInforTable").css('display', 'block');

							}

						}
						if (operation == "add") {
							$("#saveBtn").show();
							$("#saveBtn").removeAttr('disabled');
							$("#confirmBtn").hide();
							$("#baseTab").addClass("active");
							$("#baseInfor").addClass("active");
							$("#itemsTab").removeClass("active");
							$("#itemList").removeClass("active");
							$("select").removeAttr('disabled');
							$("#confirmItems").removeClass("active");
							$("#confirmItem").removeClass("active");
							$("#confirmItem").css('display', 'none');

							if (stockRetype == 1) {

								$("#itemsTable").css('display', 'block');
								$("#giftItemTable").css('display', 'none');
								$("#proSearchInfor").css('display', 'block');
								$("#giftSearchInfor").css('display', 'none');
								$("#proConfirmInfor").css('display', 'block');
								$("#giftConfirmInfor").css('display', 'none');
								$("#proInforTable").css('display', 'block');
								$("#giftInforTable").css('display', 'none');

							}
							if (stockRetype == 2) {

								$("#itemsTable").hide();
								$("#giftItemTable").css('display', 'block');
								$("#proSearchInfor").css('display', 'none');
								$("#giftSearchInfor").css('display', 'block');
								$("#proConfirmInfor").css('display', 'none');
								$("#giftConfirmInfor").css('display', 'block');
								$("#proInforTable").css('display', 'none');
								$("#giftInforTable").css('display', 'block');

							}
						}
						if (operation == "edit") {

							$("#saveBtn").show();
							$("#confirmBtn").hide();
							$("#saveBtn").removeAttr('disabled');
							$("#baseTab").addClass("active");
							$("#baseInfor").addClass("active");
							$("#itemsTab").removeClass("active");
							$("#itemList").removeClass("active");
							$("#confirmItems").removeClass("active");

							$("#confirmItem").removeClass("active");
							$("#confirmItem").css('display', 'none');
							if (stockRetype == 1) {

								$("#itemsTable").css('display', 'block')
								$("#giftItemTable").css('display', 'none');
								$("#proSearchInfor").css('display', 'block');
								$("#giftSearchInfor").css('display', 'none');
								$("#proConfirmInfor").css('display', 'block');
								$("#giftConfirmInfor").css('display', 'none');
								$("#proInforTable").css('display', 'block');
								$("#giftInforTable").css('display', 'none');

							}
							if (stockRetype == 2) {

								$("#itemsTable").hide();
								$("#giftItemTable").css('display', 'block');
								$("#proSearchInfor").css('display', 'none');
								$("#giftSearchInfor").css('display', 'block');
								$("#proConfirmInfor").css('display', 'none');
								$("#giftConfirmInfor").css('display', 'block');
								$("#proInforTable").css('display', 'none');
								$("#giftInforTable").css('display', 'block');

							}
						}
						if (operation == "confirm") {

							$("#baseTab").removeClass("active");
							$("#itemsTab").removeClass("active");
							$("#baseInfor").removeClass("active");
							$("#itemList").removeClass("active");
							
							$("#saveBtn").hide();
							$("#confirmBtn").show();
							$("#confirmBtn").removeAttr("disabled");
							if (stockAction == 0) {
								$("#baseTab").addClass("active");
								$("#baseInfor").addClass("active");
								$("#itemsTab").removeClass("active");
								$("#itemList").removeClass("active");
								$("#confirmItems").removeClass("active");
								$("#confirmItem").removeClass("active");
								$("#confirmItem").hide();
							} else {
								$("#confirmItems").addClass("active");
								$("#confirmItem").addClass("active");
								$("#baseTab").removeClass("active");
								$("#baseInfor").removeClass("active");
								$("#itemsTab").removeClass("active");
								$("#itemList").removeClass("active");
							}
							

							if (stockRetype == 1) {

								$("#itemsTable").css('display', 'block')
								$("#giftItemTable").css('display', 'none');
								$("#proSearchInfor").css('display', 'block');
								$("#giftSearchInfor").css('display', 'none');
								$("#proConfirmInfor").css('display', 'block');
								$("#giftConfirmInfor").css('display', 'none');
								$("#proInforTable").css('display', 'block');
								$("#giftInforTable").css('display', 'none');

							}
							if (stockRetype == 2) {

								$("#itemsTable").hide();
								$("#giftItemTable").css('display', 'block');
								$("#proSearchInfor").css('display', 'none');
								$("#giftSearchInfor").css('display', 'block');
								$("#proConfirmInfor").css('display', 'none');
								$("#giftConfirmInfor").css('display', 'block');
								$("#proInforTable").css('display', 'none');
								$("#giftInforTable").css('display', 'block');

							}
							// alert("stockAction---------->" + stockAction);
							if (stockAction == 1 || stockAction == 2
									|| stockAction == 3) {
								if (stockAction == 1) {
									$("#confirmHref").text("出库确认信息");
								}
								if (stockAction == 2 || stockAction == 3) {
									$("#confirmHref").text("退货确认信息");
								}
								$("#confirmItem").show();

							}

						}

					}

					$("#itemsTable")
							.delegate(
									"input[itemName='enterPrice']",
									'blur',
									function() {
										var enterPrice = $(this).val();

										var total = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='total']")
												.val();
										if (total < 0) {
											alert("数量不能为负数。");
											$(this).val("");
											return;
										}

										var salePrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='salePrice']")
												.val();

										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										// alert("rowId=" + rowId);
										var ele = detailsList[rowId];
										ele.total = total;
										ele.salePrice = salePrice;
										ele.enterPrice = enterPrice;
										if (action == 0 || action == 2
												|| action == 3) {
											ele.summery = enterPrice * total;
										}
										if (action == 1) {
											ele.summery = salePrice * total;
										}

										detailsList.splice(rowId, 1, ele);
										$("#summery_" + trId).val(ele.summery);

									});

					$("#giftItemTable")
							.delegate(
									"input[itemName='enterPrice']",
									'blur',
									function() {
										var enterPrice = $(this).val();

										var total = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='total']")
												.val();
										if (total < 0) {
											alert("数量不能为负数。");
											$(this).val("");
											return;
										}

										var salePrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='salePrice']")
												.val();

										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										// alert("rowId=" + rowId);
										var ele = detailsList[rowId];
										ele.total = total;
										ele.salePrice = salePrice;
										ele.enterPrice = enterPrice;
										if (action == 0 || action == 2
												|| action == 3) {
											ele.summery = enterPrice * total;
										}
										if (action == 1) {
											ele.summery = salePrice * total;
										}

										detailsList.splice(rowId, 1, ele);
										$("#summery_" + trId).val(ele.summery);

									});

					$("#itemsTable")
							.delegate(
									"input[itemName='salePrice']",
									'blur',
									function() {
										var salePrice = $(this).val();

										var total = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='total']")
												.val();

										var enterPrice = $(this)
												.parent()
												.parent()
												.children()
												.children(
														"input[itemName='enterPrice']")
												.val();
										if (total < 0) {
											alert("数量不能为负数。");
											$(this).val("");
											return;
										}
										var trId = $(this).parent().parent()
												.attr("id");
										var rowId = $(this).attr("rowId");

										// alert("rowId=" + rowId);
										var ele = detailsList[rowId];
										ele.total = total;
										ele.salePrice = salePrice;
										ele.enterPrice = enterPrice;
										if (action == 0 || action == 2
												|| action == 3) {
											ele.summery = enterPrice * total;
										}
										if (action == 1) {
											ele.summery = salePrice * total;
										}

										detailsList.splice(rowId, 1, ele);
										$("#summery_" + trId).val(ele.summery);
									});

					$("#proSearchInfor").delegate('[name="out"]', 'click',
							function() {

								setSuggest(this, "proSelectItem");

							});

					$("#proSearchInfor").delegate('#clearItem', 'click',
							function() {
								if (confirm("真的要清空库单列表吗?") == true) {
									clearArrAll();
									clearItemTable("itemsTable");

								}

								return;

							});

					function deleteItem(trId, tableName) {
						var delUrl = "delItemById.do";
						// AJAX DEL..
						$.ajax({
							url : delUrl,
							dataType : 'text',
							data : {
								id : trId
							},
							cache : false,

							success : function(data) {

								alert("delete Data successfully");

								$("#" + trId).remove();
								return 1;
							},
							error : function(html) {
								// alert(html);
								alert("delete Data failly");
								return 0;
							}
						});

					}

					$("#proSearchInfor")
							.delegate(
									'#proItemName',
									'keyup',
									function() {
										// alert("-------------------"
										// + $('#proItemName').val());
										if (!isReading) {

											var itemStr = $('#proItemName')
													.val();
											counter = itemStr.length;
											// alert("------itemStr-------------"
											// + itemStr);
											if (counter == 3) {

												var ajaxUrl = encodeURI("queryProducts.do?condition="
														+ itemStr);
												$
														.ajax({
															url : ajaxUrl,
															dataType : "json",
															condition : itemStr,
															success : function(
																	data) {
																// alert("data
																// is success");
																clearSuggest("proSelectItem");
																if (data != null
																		&& data.result.length > 0) {
																	for (i = 0; i < data.result.length; i++) {
																		var r = data.result[i];
																		// alert("keyup.."
																		// +
																		// r.proName);
																		createSuggest(
																				r,
																				"proSelectItem");
																	}
																	displaySuggest("proSelectItem");
																} else {
																	hiddenSuggest("proSelectItem");
																}
																isReading = false;

															},
															error : function(
																	data) {
																alert("加载数据出错");
																return;

															}
														});

											}
										} else {
											// alert("-------------------"
											// + isReading);
											return;
										}

									});

					$("#proSearchInfor")
							.delegate(
									'#proItemName',
									'change',
									function() {

										if (!isReading) {

											var itemStr = $('#proItemName')
													.val();
											var ajaxUrl = encodeURI("queryProducts.do?condition="
													+ itemStr);
											$
													.ajax({
														url : ajaxUrl,
														dataType : "json",
														condition : itemStr,
														success : function(data) {
															// alert("data is
															// success");
															if (data != null
																	&& data.result.length > 0) {
																clearSuggest("proSelectItem");
																for (i = 0; i < data.result.length; i++) {
																	var r = data.result[i];

																	createSuggest(
																			r,
																			"proSelectItem");
																}
																displaySuggest("proSelectItem");
															} else {
																hiddenSuggest("proSelectItem");
															}
															isReading = false;

														},
														error : function(data) {
															alert("加载数据出错");
															return;

														}
													});

										} else {
											alert("isReading" + isReading);
										}

									});

					$("#giftSearchInfor").delegate('[name="out"]', 'click',
							function() {

								setSuggest(this, "giftSelectItem");

							});

					$("#giftSearchInfor").delegate('#clearItem', 'click',
							function() {
								if (confirm("真的要清空库单列表吗?") == true) {
									clearArrAll();
									clearItemTable("giftItemTable");
									return;
								}

							});

					$("#giftSearchInfor")
							.delegate(
									'#giftItemName',
									'keyup',
									function() {
										if (!isReading) {

											var itemStr = $(this).val();
											counter = itemStr.length;
											if (itemStr == "") {
												return -1;
											}
											if (counter == 3) {

												var ajaxUrl = encodeURI("queryGifts.do?condition="
														+ itemStr);
												$
														.ajax({
															url : ajaxUrl,
															dataType : "json",
															condition : itemStr,
															success : function(
																	data) {
																if (data != null
																		&& data.result.length > 0) {
																	for (i = 0; i < data.result.length; i++) {
																		var r = data.result[i];
																		// alert("key
																		// up.."
																		// +
																		// result.proName);
																		createSuggest(
																				r,
																				"giftSelectItem");
																	}
																	displaySuggest("giftSelectItem");
																} else {
																	hiddenSuggest("giftSelectItem");
																}
																isReading = false;

															},
															error : function(
																	data) {
																alert("加载数据出错");
																return;

															}
														});

											}
										}

									});

					$("#giftSearchInfor")
							.delegate(
									'#giftItemName',
									'change',
									function() {
										if (!isReading) {
											var itemStr = $(this).val();

											var ajaxUrl = encodeURI("queryGifts.do?condition="
													+ itemStr);
											$
													.ajax({
														url : ajaxUrl,
														dataType : "json",
														condition : itemStr,
														success : function(data) {
															if (data != null
																	&& data.result.length > 0) {
																clearSuggest("giftSelectItem");
																for (i = 0; i < data.result.length; i++) {
																	var result = data.result[i];

																	createSuggest(
																			result,
																			"giftSelectItem");
																}
																displaySuggest("giftSelectItem");
															} else {
																hiddenSuggest("giftSelectItem");
															}
															isReading = false;

														},
														error : function(data) {
															alert("加载数据出错");
															return;

														}
													});

										}

									});

					$("#baseInfor").delegate('#orderSource', 'change',
							function() {
								$("#isFromOrder").val($(this).val());
							});

					$("#giftSearchInfor")
							.delegate(
									'#giftAddItem',
									'click',
									function() {
										var itemId = $("#giftItemId").val();
										// var stockId = $("#proId").val();

										var loadStockUrl = "loadGiftDetail.do?id="
												+ itemId;
										if (itemId == null || itemId == ""
												|| itemId == "undefined") {
											alert("请选择规格");
											return;
										}
										// alert("itemId=" + itemId);
										$
												.ajax({
													url : loadStockUrl,
													dataType : "json",
													id : itemId,
													success : function(data) {
														// alert("successfully");
														if (data != null) {

															var itemTable = $("#giftItemTable");

															if (isItemInOrder(
																	itemTable,
																	data) <= 0) {

																addProductDetail(data);
																alert("规格:【"
																		+ data.ruleCode
																		+ "】"
																		+ data.ruleName
																		+ "成功加入单据中");
															} else {
																alert("规格:【"
																		+ data.ruleCode
																		+ "】"
																		+ data.ruleName
																		+ "已存在于单据中");

															}

															return;
														}

													},
													error : function(data) {
														alert("加载数据出错");
														return;

													}
												});

									});

					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#stocksDialog").on('hidden.bs.modal', function() {
						// $(this).removeData("bs.modal");
						// alert("close modal Dialog");

					});

					$("#stocksDialog").on('hide.bs.modal', function() {
						// $(this).removeData("bs.modal");
						// alert("closing modal Dialog");

						clearArrAll();
						clearConfirmDetails();
						clearItemTable("itemsTable");
						clearItemTable("giftItemTable");
						$("#baseTab").addClass("active");
						$("#baseInfor").addClass("active");
						$("#itemsTab").removeClass("active");
						$("#itemList").removeClass("active");
						$("#confirmItem").removeClass("active");
						$("#items").removeClass("active");
						clearItemTable("proInforTable");
						clearItemTable("giftInforTable");

					});

					$("#proSearchInfor")
							.delegate(
									'#proAddItem',
									'click',
									function() {

										var itemId = $("#proItemId").val();
										var stockId = $("#proId").val();
										var reTypeId = $("#reType").val()
										var loadStockUrl = "loadProduct.do?id="
												+ itemId;
										if (itemId == null || itemId == ""
												|| itemId == "undefined") {
											alert("请选择规格")
											return;
										}

										$
												.ajax({
													url : loadStockUrl,
													dataType : "json",
													id : itemId,
													success : function(data) {

														if (data != null) {

															var itemTable = $("#itemsTable");
															if (isItemInOrder(
																	itemTable,
																	data) <= 0) {

																addProductDetail(data);
																alert("规格:【"
																		+ data.ruleCode
																		+ "】"
																		+ data.ruleName
																		+ "成功加入单据中");
															} else {
																alert("规格:【"
																		+ data.ruleCode
																		+ "】"
																		+ data.ruleName
																		+ "已存在于单据中");

															}

														}

													},
													error : function(data) {

													}

												});

									});

					/**
					 * 加入规格条目
					 */
					function addProductDetail(data) {
						detailsList.push(data);
						// alert("addProductDetail===" + data.proId);
						len = detailsList.length;
						getQuery(len);// 分页

					}

					function computePageCount(itemTotalCount, itemPSize) {

						if (itemTotalCount <= itemPSize) {
							itemPageCount = 1;
						} else if (itemTotalCount % itemPSize == 0) {
							itemPageCount = (itemTotalCount / itemPSize);
						} else {
							itemPageCount = parseInt(itemTotalCount / itemPSize) + 1;
						}
						return itemPageCount;
					}
					/**
					 * 确认单据
					 */
					$("#optBtn").delegate(
							'#confirmBtn',
							'click',
							function() {
								var selectItem = null;
								var typeId = $("#reType").val();
								var actionId = $("#action").val();
								var proId = $("#proId").val();
								 isScanCode = false;
								$("#status").val("1");
								// alert("typeId=" + typeId);
								if (typeId == 1) {
									clearItemTable("proInforTable");

									selectItem = $("#proInforTable");
								}
								if (typeId == 2) {
									clearItemTable("giftInforTable");
									selectItem = $("#giftInforTable");
								}

								$("#stockItems").attr("method", "post");
								$("#stockItems").attr("action", "update.do");
								if (actionId != 0) {
									if (comfirmDetails != null
											&& comfirmDetails.length > 0) {
										submitStockConfirm(comfirmDetails,
												selectItem);
									} else {
										alert("尚未录入库单确认信息，请扫描产品/礼品条形码");
										return;
									}
								} else {
									if (proId != null && proId != "") {

										$("#stockItems").submit();
									}
								}

							});

					function submitStockConfirm(comfirmDetails, selectItem) {
						var checkStockUrl = "checkOutStockConfirm.do";
						var snCodeList = new Array();

						for (i = 0; i < comfirmDetails.length; i++) {
							var cdetail = comfirmDetails[i];
							snCodeList.push(cdetail.snCode);
						}

						var itemId = $("#proId").val();

						$
								.ajax({
									url : checkStockUrl,
									traditional : true,
									dataType : "json",
									contentType : 'application/json',
									data : {
										id : itemId,
										confirms : snCodeList
									},

									success : function(data) {
										alert("success: " + data.msg);
										if (data.msg == "success") {

											for (i = 0; i < comfirmDetails.length; i++) {
												addProductConfirmContent(
														selectItem,
														comfirmDetails[i], i);

											}
											$("#status").val("1");

											$("#stockItems").submit();

										} else {
											alert("存在不正确的条形码");
										}
									},
									error : function(data) {
										alert("ajax error...");
									}
								});

					}

					/**
					 * 保存单据
					 */
					$("#optBtn").delegate(
							'#saveBtn',
							'click',
							function() {
								var selectItem = null;
								var typeId = $("#reType").val();
								 isScanCode = false;
								// alert("typeId=" + typeId);
								if (typeId == 1) {
									clearItemTable("itemsTable");

									selectItem = $("#itemsTable");
								}
								if (typeId == 2) {
									clearItemTable("giftItemTable");
									selectItem = $("#giftItemTable");
								}

								$("#stockItems").attr("method", "post");
								var itemArr = [];
								var proId = $("#proId").val();

								if (proId == "" || proId == null
										|| proId == "undefined") {

									$("#stockItems").attr("action", "save.do");
									// alert("save");
									for (i = 0; i < detailsList.length; i++) {
										addItemContent(selectItem,
												detailsList[i], i);
									}
								} else {
									$("#stockItems")
											.attr("action", "update.do");
									// alert("update");
									for (i = 0; i < detailsList.length; i++) {
										loadDetail(selectItem, detailsList[i],
												i);
									}

								}
								if ($("#status").val() != 1) {
									$("#status").val("0");
								}
								$("#stockItems").submit();

							});

					/**
					 * 防止表单提交
					 * 
					 */
					$("#stockItems")
							.delegate(
									$(this),
									"submit",
									function() {
										if(isScanCode==true){
										var snCode=-1 ;
										var typeId = $("#reType").val();
										if (typeId == 1) {
											 snCode = $("#proSnCode").val();
										}
										if (typeId == 2) {
											snCode = $("#giftSnCode").val();
										}

										if (proSnCode != null
												&& proSnCode != ""
												&& proSnCode != "undefined") {
											var queryUrl = basePath
													+ "/manage/stocksDetail/queryStockDetailByCode.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															snCode : snCode,
															reType : typeId
														},
														cache : false,

														success : function(data) {
															// addProductConfirm(data)
															
															if (typeId == 1) {
																addProductConfirm(data);
															}
															if (typeId == 2) {
																addGiftConfirm(data);
															}
															return false;
														},
														error : function(html) {
															
															alert("load Data failly");
															return false;
														}
													});
										}
										return !isScanCode;
										}
										return true;

									});

					$('#pagination2')
							.jqPaginator(
									{
										totalPages : pageCount,
										visiblePages : pageCount,
										currentPage : 1,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											// alert("pageIndex="+num);
											// alert("type="+type);
											$("#pageIndex").val(num);
											var typeId = $("#reTypeIds").val();
											var actStr = $("#actStr").val();
											// alert("type----Id=" + typeId);
											// alert("actStr----Id=" + actStr);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															retype : typeId,
															actStr : actStr
														},
														cache : false,
														success : function(data) {

															clearItemTable("products");
															var results = data.pageResult.resultList;
															var actionArr = data.actions;
															var typeArr = data.reTypes;

															for (var i = 0; i < results.length; i++) {
																var stock = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='proId' id='proId' class='checkbox'"
																		+ "type='checkbox' value="
																		+ stock.id
																		+ " /></td>"
																		+ "<td>"
																		+ stock.stockCode
																		+ "</td>";

																for (var j = 0; j < typeArr.length; j++) {
																	var type = typeArr[j];
																	if (type.key == stock.reType) {
																		rowContent = rowContent
																				+ "<td>"
																				+ type.val
																				+ "</td>";
																		break;
																	}
																}

																rowContent = rowContent
																		+ "<td>"
																		+ stock.creator
																		+ "</td>"
																		+ "<td>"
																		+ stock.createTime
																		+ "</td>"
																		+ "<td>"
																		+ stock.comfirmer
																		+ "</td>"
																		+ "<td>"
																		+ stock.comfirmTime
																		+ "</td>";
																for (var k = 0; k < actionArr.length; k++) {
																	var action = actionArr[k];
																	if (action.key == stock.action) {
																		rowContent = rowContent
																				+ "<td>"
																				+ action.val
																				+ "</td>";
																		break;
																	}
																}

																rowContent = rowContent
																		+ "<td>"
																		+ stock.summery
																		+ "</td>";
																if (stock.status == 0) {
																	// alert(0);
																	rowContent = rowContent
																			+ "<td>"
																			+ "<button name='editBtn' objId="
																			+ stock.id
																			+ " class='btn btn-sm-block'>修改</button></td>";
																	rowContent = rowContent
																			+ "<td>"
																			+ "<button name='confirmItem' objId="
																			+ stock.id
																			+ " actionId="
																			+ stock.action
																			+ "  class='btn btn-sm btn-danger'>确认订单</button></td>";
																	+"</tr>";
																}
																if (stock.status == 1) {

																	rowContent = rowContent
																			+ "<td>"
																			+ "<button name='viewBtn' objId="
																			+ stock.id
																			+ " class='btn btn-sm-block'>查看</button></td>";
																	rowContent = rowContent
																			+ "<td>已确定</td></tr>";
																}
																$("#products")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 清空确认信息
					 */
					function clearConfirmDetails() {
						if (comfirmDetails.length > 0) {
							comfirmDetails.splice(0, comfirmDetails.length);
							confirmRowNum = 0;
						}

					}

					function loadConfirmDetails(stockId) {

						var editUrl = "queryConfirmByStockId.do";

						var reType = $("#reType").val();

						if (reType == 1) {
							clearItemTable("proInforTable");
						}
						if (reType == 2) {

							clearItemTable("giftInforTable");
						}

						$.ajax({

							url : editUrl,
							dataType : 'json',

							data : {
								id : stockId
							},

							cache : false,

							success : function(data) {
								var details = data.confirmDetails;
								if (details.length == 0) {
									getConfirmQuery(comfirmDetails.length);
								} else {
									clearConfirmDetails();
									if (details != null) {
										for (i = 0; i < details.length; i++) {
											comfirmDetails.push(details[i]);
										}

										getConfirmQuery(comfirmDetails.length);
									}
								}

							},
							error : function(html) {
								// alert(html);
								alert("加载详细信息失败.");
								return;
							}
						});
					}
					/**
					 * 加载已扫条形码列表
					 */

					$("#stockItems").delegate("#confirmItem", 'click',
							function() {

								var stockId = $("#proId").val();
								loadConfirmDetails(stockId);

							});

					function laodStockDetails() {
						var editUrl = "queryDetailByStockId.do";
						// alert("len---------------->"+len);
						var stockId = $("#proId").val();
						var reType = $("#reType").val();
						if (reType == 1) {
							clearItemTable("itemsTable");
						}
						if (reType == 2) {
							clearItemTable("giftItemTable");
						}
						// alert("stockId-------------->"+stockId)
						if (stockId != null && stockId != ""
								&& stockId != "undefined") {

							$.ajax({

								url : editUrl,
								dataType : 'json',
								data : {
									id : stockId
								},
								cache : false,

								success : function(data) {
									var details = data.details;
									clearArrAll();
									if (details != null) {
										for (i = 0; i < details.length; i++) {
											detailsList.push(details[i]);
										}
										len = detailsList.length;
										getQuery(len);
									}

								},
								error : function(html) {
									// alert(html);
									alert("加载详细信息失败.");
									return;
								}
							});
						} else {
							len = detailsList.length;
							// alert("len---------------->"+len);
							getQuery(len);
						}
					}

					$("#stockItems").delegate("#itemsTab", 'click', function() {
						laodStockDetails();
					});

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {
								var proId = $(this).attr("objId");
								isView = false;
								$("#operation").val("edit");
								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId
									},
									cache : false,

									success : function(data) {

										var selectItem = null
										var itemTable = null;
										if (data.reType == 1) {
											// alert("data.reType==1");
											selectItem = "itemsTable";
											itemTable = $("#itemsTable");
										}
										if (data.reType == 2) {
											// alert("data.reType==2");
											selectItem = "giftItemTable";
											itemTable = $("#giftItemTable");
										}
										var itemList = data.details;

										if (itemList != null) {

											itemTotalCount = itemList.length;

											clearItemTable(selectItem);

										}
										loadStock(data, "edit");

									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										return;
									}
								});

							});

					$("#products").delegate('[name="viewBtn"]', 'click',
							function() {
								var proId = $(this).attr("objId");
								isView = true;
								$("#operation").val("view");
								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId
									},
									cache : false,

									success : function(data) {

										var selectItem = "itemsTable";
										itemTable = $("#itemsTable");
										var itemList = data.details;

										if (itemList != null) {

											itemTotalCount = itemList.length;

											clearItemTable(selectItem);

										}

										loadStock(data, "view");

									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										return;
									}
								});

							});

					$("#giftItemTable")
							.delegate(
									"button[name='delItem']",
									'click',
									function() {
										if (confirm("是否要删除该记录?") == true) {
											var trId = $(this).attr("objId");
											var id = $(this).parent().parent()
													.attr("id");
											// alert("trId=" + trId)

											if (trId == null && trId == ""
													&& trId == "undefined") {
												detailsList.splice(trId, 1);
												$(this).parent().parent()
														.remove();
												len = detailsList.length;
												getQuery(len);

											} else {

												var delUrl = "delItemById.do";
												$
														.ajax({
															url : delUrl,
															dataType : 'text',
															data : {
																id : id
															},
															cache : false,

															success : function(
																	data) {
																alert("delete Data successfully");
																detailsList
																		.splice(
																				id,
																				1);
																clearItemTable("giftItemTable");
																len = detailsList.length;
																getQuery(len);
																return;
															},
															error : function(
																	html) {
																alert("delete Data failly");
																return;
															}
														});

											}

										}

									});

					$("#itemsTable")
							.delegate(
									"button[name='delItem']",
									'click',
									function() {
										if (confirm("是否要删除该记录?") == true) {
											var trId = $(this).attr("objId");
											var id = $(this).parent().parent()
													.attr("id");
											alert("trId=" + trId);
											alert("id=" + id);
											if (trId == null || trId == ""
													|| trId == "undefined") {
												detailsList.splice(trId, 1);
												clearItemTable("itemsTable");
												len = detailsList.length;
												getQuery(len);

											} else {
												var delUrl = "delItemById.do";
												$
														.ajax({
															url : delUrl,
															dataType : 'text',
															data : {
																id : trId
															},
															cache : false,

															success : function(
																	data) {
																alert("delete Data successfully");
																detailsList
																		.splice(
																				id,
																				1);
																clearItemTable("itemsTable");
																len = detailsList.length;
																getQuery(len);
																return;
															},
															error : function(
																	html) {
																alert("delete Data failly");
																return;
															}
														});

											}

										}

									});

					/**
					 * 清空数组
					 */
					function clearArrAll() {
						if (detailsList.length > 0) {
							detailsList.splice(0, detailsList.length);
						}
					}

					/**
					 * JS 初始化分页组件
					 */
					function getQuery(len) {
						/**
						 * 添加的内分页
						 */

						// alert("len-------------" + len);
						itemTotalCount = len;

						if (len <= itemPSize) {
							addPageCount = 1;
						} else {
							if (len % itemPSize == 0) {
								addPageCount = len / itemPSize;
							} else {
								addPageCount = Math.floor((len / itemPSize)) + 1;
							}

						}
						alert("itemPageCount---------" + addPageCount);
						$('#itemPagination')
								.jqPaginator(
										{
											totalPages : addPageCount,
											visiblePages : 10,
											currentPage : 1,
											prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
											page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
											next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
											onPageChange : function(num, type) {

												var itemTable;
												var reType = $("#reType").val();
												// alert("reType=" + reType);
												if (reType == 1) {
													clearItemTable("itemsTable");
													itemTable = $("#itemsTable");
												}
												if (reType == 2) {
													alert("gift");
													clearItemTable("giftItemTable");
													itemTable = $("#giftItemTable");
												}
												// alert("itemTable="+itemTable)
												var start = num * itemPSize
														- itemPSize;
												var end = num * itemPSize;
												// rowNum++;
												var proId = $("#proId").val();
												// alert("proId-------------"+
												// proId);
												var idx = 0;
												// alert("start---------" +
												// start);
												for (i = start; i < detailsList.length; i++) {
													if (idx < 5) {
														if (proId == null
																|| proId == "") {
															// alert("proId-------------add");

															addItemContent(
																	itemTable,
																	detailsList[i],
																	i);

														} else {
															// alert("proId-------------look");
															loadDetail(
																	itemTable,
																	detailsList[i],
																	i);
														}
														idx++;
													}
												}
											}
										});
						return true;
					}

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="proId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#tools").delegate('#addEnter', 'click', function() {

						var actStr = "enter";

						addStocks(actStr)

					});
					$("#tools").delegate('#addOut', 'click', function() {

						var actStr = "out";
						// alert("******proId******"+typeId);
						addStocks(actStr)

					});

					$("#tools").delegate('#addBack', 'click', function() {
						var actStr = "back";
						// alert("******proId******"+typeId);
						addStocks(actStr)

					});

					/**
					 * 根据传入的table ID 清空页面上table显示的数据
					 */
					function clearItemTable(selectItem) {

						$("#" + selectItem + " tr:not(:first)").remove();
						rowNum = 0;
					}

					function addStocks(actStr) {
						isView = false;
						clearItemTable("itemsTable");
						clearItemTable("giftItem");
						var editUrl = "add.do";
						$("#operation").val("add");
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								action : actStr
							},
							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								// clearArr----------
								clearArrAll();
								$("#stockItems input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});

								$("#creator").prop("readonly", true);
								$("#createTime").prop("readonly", true);

								$("#stockItems textarea").each(function() {
									$(this).prop("value", null);
								});

								loadStock(data, "add");

							},
							error : function(html) {
								// alert(html);
								alert("Load Data failly");
								refresh();
							}
						});
					}

					$('#tools').delegate('#queryBtn', 'click', function() {

						var reType = $("#reTypeIds").val();
						var actStr = $("#actStr").val();

						// alert("reType=" + reType);
						// alert("actStr=" + actStr);
						queryStock(reType, actStr);

					});

					/**
					 * 查询库存
					 */
					function queryStock(reType, actStr) {

						var queryUrl = "queryByPage.do";
						$
								.ajax({
									url : queryUrl,
									dataType : 'json',
									data : {
										pageIndex : pageIndex,
										pageSize : pSize,
										retype : reType,
										actStr : actStr
									},
									cache : false,
									success : function(data) {

										$("#products tr:not(:first)").remove();
										var results = data.pageResult.resultList;
										var actionArr = data.actions;
										var typeArr = data.reTypes;
										// alert("fuck you length:+
										// results.length)
										// return;
										for (var i = 0; i < results.length; i++) {

											var stock = results[i];
											// alert(stock.id);
											// alert(stock.stockCode);

											var rowContent = "<tr>"
													+ "<td><input name='proId' id='proId' class='checkbox'"
													+ "type='checkbox' value="
													+ stock.id + " /></td>"
													+ "<td>" + stock.stockCode
													+ "</td>";
											//
											for (var j = 0; j < typeArr.length; j++) {
												var type = typeArr[j];
												if (type.key == stock.reType) {
													rowContent = rowContent
															+ "<td>" + type.val
															+ "</td>";
													break;
												}
											}

											rowContent = rowContent + "<td>"
													+ stock.creator + "</td>"
													+ "<td>" + stock.createTime
													+ "</td>" + "<td>"
													+ stock.comfirmer + "</td>"
													+ "<td>"
													+ stock.comfirmTime
													+ "</td>";

											for (var k = 0; k < actionArr.length; k++) {
												var action = actionArr[k];
												if (action.key == stock.action) {
													rowContent = rowContent
															+ "<td>"
															+ action.val
															+ "</td>";
													break;
												}

											}

											rowContent = rowContent + "<td>"
													+ stock.summery + "</td>";
											if (stock.status == 0) {
												// alert(0);
												rowContent = rowContent
														+ "<td>"
														+ "<button name='editBtn' objId="
														+ stock.id
														+ " class='btn btn-sm-block'>修改</button></td>";
												rowContent = rowContent
														+ "<td>"
														+ "<button name='confirmItem' objId="
														+ stock.id
														+ " class='btn btn-sm btn-danger' actionId="
														+ stock.action
														+ ">确认订单</button></td>";
												+"</tr>";
											}
											if (stock.status == 1) {

												rowContent = rowContent
														+ "<td>"
														+ "<button name='viewBtn' objId="
														+ stock.id
														+ " class='btn btn-sm-block'>查看</button></td>";
												rowContent = rowContent
														+ "<td>已确定</td></tr>";
											}

											$("#products").append(rowContent);
										}
									},
									error : function(html) {
										return;
									}
								});

					}

					$("#products").delegate("button[name='confirmItem']",
							'click', function() {

								var proId = $(this).attr("objId");
								var actionId = $(this).attr("actionId");
								
								$("#operation").val("confirm");
								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId
									},
									cache : false,

									success : function(data) {

										var selectItem = null
										var itemTable = null;
										if (data.reType == 1) {
											// alert("data.reType==1");
											selectItem = "itemsTable";
											itemTable = $("#itemsTable");
										}
										if (data.reType == 2) {
											// alert("data.reType==2");
											selectItem = "giftItemTable";
											itemTable = $("#giftItemTable");
										}
										var itemList = data.details;

										if (itemList != null) {

											itemTotalCount = itemList.length;

											clearItemTable(selectItem);

										}
										loadStock(data, "confirm");

									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										return;
									}
								});

							});

					function resetReading() {
						if (!isReading) {
							isReading = true;
							readInfo();
						}
					}

					function readInfo() {
						var info = document.getElementById("proItemName").value;
						if (currentInfo == info && info != "") {
							counter++;
						} else {
							currentInfo = info;
							counter = 1;
						}
						if (counter == 3) {
							getSuggest(info);
							isReading = false;
						} else {
							setTimeout("readInfo()", 200);
						}
					}

					/**
					 * 加载库单信息
					 */
					function loadStock(stock, operation) {

						//alert("action---------->" + stock.creator);
						var stockId = stock.id;

						var retypeId = stock.reType;
						var actionId = stock.action;
						// 加载详细信息
						$("#proId").val(stock.id);
						$("#stockCode").val(stock.stockCode);
						$("#summery").val(stock.summery);
						$("#creator").val(stock.creator);
						$("#createTime").val(stock.createTime);
						$("#comfirmer").val(stock.comfirmer);
						$("#comfirmTime").val(stock.comfirmTime);
						$("#reType").val(stock.reType);
						$("#reTypes").val(stock.reType);
						$("#isFromOrder").val(stock.isFromOrder);
						$("#orderSource").val(stock.isFromOrder);
						$("#orderId").val(stock.orderId);
						$("#orderCode").val(stock.orderCode);
						$("#status").val(stock.status);
						$("#actions").val(stock.action);
						$("#action").val(stock.action);
						$("#note").val(stock.note);
						laodStockDetails();

						if (operation == "add") {

							$("#stockItems input").each(function() {

								$(this).prop("readonly", false);

							});

							changeComponentsVisiable(null, retypeId, actionId,
									operation);
						}
						if (operation == "edit") {

							changeComponentsVisiable(stockId, retypeId,
									actionId, operation);
						}
						if (operation == "confirm") {
							
							if (actionId != 0) {
								loadConfirmDetails(stockId);
							}
							changeComponentsVisiable(stockId, retypeId,
									actionId, operation);
						}
						if (operation == "view") {

							changeComponentsVisiable(stockId, retypeId,
									actionId, operation);
						}

						$("#stocksDialog").modal("show");

					}

					$('#proSearchInfor').delegate('#proRules', 'change',
							function() {
								$("#proItemId").val($(this).val());

							});

					$('#giftSearchInfor').delegate('#giftRules', 'change',
							function() {
								$("#giftItemId").val($(this).val());
							});

					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
					}

					/**
					 * 选定规格后，把规格的ID写入隐藏域中，把名称显示在文本框中
					 */
					function setSuggest(src, selectItem) {
						// alert("setSuggest" + src.id);
						if ("proSelectItem" == selectItem) {
							document.getElementById("proItemName").value = src.innerHTML;
							// document.getElementById("proItemId").value =
							// src.id;
							document.getElementById("proInfoId").value = src.id;
							queryProductDetail(document
									.getElementById("proInfoId").value);

						}
						if ("giftSelectItem" == selectItem) {
							document.getElementById("giftItemName").value = src.innerHTML;
							// document.getElementById("giftItemId").value =
							// src.id;
							document.getElementById("giftInforId").value = src.id;
							// alert("------------->
							// "+document.getElementById("giftItemId").value);
							queryGiftDetail(document
									.getElementById("giftInforId").value);
						}
						hiddenSuggest(selectItem);
					}

					// 查询产品规格信息
					function queryGiftDetail(giftId) {
						// alert("----------giftId=" + giftId);
						var ajaxUrl = "loadGiftDetails.do";
						$
								.ajax({
									url : ajaxUrl,
									dataType : "json",
									data : {
										id : giftId
									},
									success : function(data) {
										// alert("data is success");
										if (data != null
												&& data.result.length > 0) {
											clearSuggest("giftSelectItem");
											for (i = 0; i < data.result.length; i++) {
												var r = data.result[i];

												// 规格列表
												var optStr = "<option value='"
														+ r.id + "'>"
														+ r.gdName
														+ "</option>";
												$("#giftRules").append(optStr);
											}
											displaySuggest("giftSelectItem");
										} else {
											hiddenSuggest("giftSelectItem");
										}
										isReading = false;

									},
									error : function(data) {
										alert("加载数据出错");
										return;

									}
								});
					}

					function queryProductDetail(proId) {
						// alert("---queryProductDetail-------proId=" + proId);
						var ajaxUrl = "loadProDetail.do";
						$.ajax({
							url : ajaxUrl,
							dataType : "json",
							data : {
								id : proId
							},
							success : function(data) {
								// alert("data is success");
								if (data != null && data.result.length > 0) {
									clearSuggest("proSelectItem");
									for (i = 0; i < data.result.length; i++) {
										var r = data.result[i];

										// 规格列表
										var optStr = "<option value='" + r.id
												+ "'>" + r.ruleName
												+ "</option>";
										$("#proRules").append(optStr);
									}
									displaySuggest("proSelectItem");
								} else {
									hiddenSuggest("proSelectItem");
								}
								isReading = false;

							},
							error : function(data) {
								alert("加载数据出错");
								return;

							}
						});

					}

					// 显示提示信息
					function displaySuggest(selectItem) {
						// alert("displaySuggest" + selectItem);

						document.getElementById(selectItem).style.display = "";
					}
					// 隐藏提示信息
					function hiddenSuggest(selectItem) {

						document.getElementById(selectItem).style.display = "none";
					}
					// 清空提示信息
					function clearSuggest(selectItem) {
						// alert("clearSuggest" + selectItem);
						document.getElementById(selectItem).innerHTML = "";
					}

					/**
					 * 根据不同单据类型，把查出来的规格数据集合放入select组件中
					 */
					function createSuggest(data, selectItem) {

						if ("proSelectItem" == selectItem) {
							var sDiv = "<div name='out' class='out'onmouseover=\"this.className='over'\""
									+ "onmouseout=\"this.className='out'\" id='"
									+ data.id + "'>" + data.proName + "</div>";
							document.getElementById("proSelectItem").innerHTML += sDiv;

							// 规格列表
							// var optStr = "<option value='" + data.id + "'>"
							// + data.ruleName + "</option>";
							// $("#proRules").append(optStr);
						}
						if ("giftSelectItem" == selectItem) {

							var sDiv = "<div name='out' class='out'onmouseover=\"this.className='over'\""
									+ "onmouseout=\"this.className='out'\" id='"
									+ data.id + "'>" + data.giftName + "</div>";
							document.getElementById("giftSelectItem").innerHTML += sDiv;

							// 规格列表
							// var optStr = "<option value='" + data.id + "'>"
							// + data.ruleName + "</option>";
							// $("#giftRules").append(optStr);
						}

					}

					/**
					 * 检查单子是否存在重复条目
					 */
					function isItemInOrder(itemTable, data) {

						var j = 0;
						for (var i = 0; i < detailsList.length; i++) {
							var g = detailsList[i];
							if (g == null || g == "" || g == "undefined") {
								continue;
							}

							if (data.ruleCode == g.ruleCode) {
								j++;
							}

						}
						return j;

					}

					function addItemContent(itemTable, data, rowNum) {
						var reType = $("#reType").val();
						if (reType == 1) {
							var rowContent = "<tr id="
									+ rowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ rowNum + "'  name='details[" + rowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + rowNum
									+ "' type='hidden' name='details[" + rowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' />" +

									"<input id='proName_" + rowNum
									+ "' type='text' name='details[" + rowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "'/><input id='ruleName_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName + "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='enterPrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].enterPrice' itemName='enterPrice' value='"
									+ data.enterPrice + "'rowId='" + rowNum
									+ "' readonly='readonly'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='salePrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' rowId='" + rowNum
									+ "' /></td>";
							if (data.total == null || data.total == 'undefined') {
								data.total = 0;
							}
							if (data.summery == null
									|| data.summery == 'undefined') {
								data.summery = 0;
							}

							if (data.note == null || data.note == 'undefined') {
								data.note = "";
							}
							rowContent = rowContent
									+ "<td style='text-align: center;'><input  id='total_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].total' itemName='total' value='"
									+ data.total + "' rowId='" + rowNum
									+ "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='summery_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].summery' itemName='summery' value='"
									+ data.summery + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='note_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].note' itemName='note' value='"
									+ data.note + "'  rowId='" + rowNum
									+ "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><button  class='btn btn-sm btn-danger' objId='"
									+ rowNum
									+ "'  name='delItem' type='button'>删除</button></td>";
							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
							rowNum++;
						}
						if (reType == 2) {
							// alert("---------------reType=" + reType);
							var rowContent = "<tr id="
									+ rowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ rowNum + "'  name='details[" + rowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode + "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + rowNum
									+ "' type='hidden' name='details[" + rowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' />"
									+ "<input id='proName_" + rowNum
									+ "' type='text' name='details[" + rowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName + "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "'/><input id='ruleName_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName + "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='enterPrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].enterPrice' itemName='enterPrice' value='"
									+ data.enterPrice + "'rowId='" + rowNum
									+ "' readonly /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='salePrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' rowId='" + rowNum
									+ "' /></td>";
							if (data.total == null || data.total == 'undefined') {
								data.total = 0;
							}
							if (data.summery == null
									|| data.summery == 'undefined') {
								data.summery = 0;
							}

							if (data.note == null || data.note == 'undefined') {
								data.note = "";
							}
							rowContent = rowContent
									+ "<td style='text-align: center;'><input  id='total_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].total' itemName='total' value='"
									+ data.total + "' rowId='" + rowNum
									+ "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='summery_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].summery' itemName='summery' value='"
									+ data.summery + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='note_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].note' itemName='note' value='"
									+ data.note + "'  rowId='" + rowNum
									+ "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><button  class='btn btn-sm btn-danger' objId='"
									+ rowNum
									+ "'  name='delItem' type='button'>删除</button></td>";
							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
							rowNum++;

						}

						if (isView) {
							// readonly
							$("#stocksDialog input").attr("readonly",
									'readonly');
							$("#stocksDialog textarea").attr("readonly",
									'readonly');
							$("#stocksDialog select").attr("disabled",
									'disabled');
							$("#saveBtn").attr("disabled", 'disabled');
							$("#confirmBtn").attr("disabled", 'disabled');
							$("#itemsTable").attr("disabled", 'disabled');
							$("#giftItem").attr("disabled", 'disabled');
							$("#giftclearItem").attr("disabled", 'disabled');
							$("#proclearItem").attr("disabled", 'disabled');
							$("#giftAddItem").attr("disabled", 'disabled');
							$("#proAddItem").attr("disabled", 'disabled');

						} else {
							$("#stocksDialog input").removeAttr("readonly");
							$("#stocksDialog textarea").removeAttr("readonly");
							$("#stocksDialog select").removeAttr('disabled');
							$("#saveBtn").removeAttr("disabled");
							$("#confirmBtn").removeAttr("disabled");
							$("#itemsTable").removeAttr("disabled");
							$("#giftItem").removeAttr("disabled");
							$("#giftclearItem").removeAttr("disabled");
							$("#proclearItem").removeAttr("disabled");
							$("#giftAddItem").removeAttr("disabled");
							$("#proAddItem").removeAttr("disabled");
						}

					}

					function loadDetail(itemTable, data, rowNum) {

						var reType = $("#reType").val();
						if (reType == 1) {
							var rowContent = "<tr id="
									+ rowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ rowNum + "'  name='details[" + rowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode + "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + rowNum
									+ "' type='hidden' name='details[" + rowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' />" + "<input id='id_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum + "].id' itemName='id' value='"
									+ data.id + "' />" + "<input id='proName_"
									+ data.id + "' type='text' name='details["
									+ rowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName + "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "'/>"
									+ "<input id='ruleName_" + rowNum
									+ "' type='text' name='details[" + rowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName + "'/></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='enterPrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].enterPrice' itemName='enterPrice' value='"
									+ data.enterPrice + "'rowId='" + rowNum
									+ "' readonly='readonly'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='salePrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' rowId='" + rowNum
									+ "'/></td>";
							if (data.total == null || data.total == 'undefined') {
								data.total = 0;
							}
							if (data.summery == null
									|| data.summery == 'undefined') {
								data.summery = 0;
							}

							if (data.note == null || data.note == 'undefined') {
								data.note = "";
							}
							rowContent = rowContent
									+ "<td style='text-align: center;'><input  id='total_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].total' itemName='total' rowId='"
									+ rowNum + "' value='" + data.total
									+ "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='summery_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].summery' itemName='summery' value='"
									+ data.summery + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='note_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].note' itemName='note' value='"
									+ data.note + "'  rowId='" + rowNum
									+ "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><button  class='btn btn-sm btn-danger' objId='"
									+ data.id
									+ "'  name='delItem' type='button'>删除</button></td>";
							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
							rowNum++;
						}
						/**
						 * 礼物
						 */
						if (reType == 2) {
							// alert("---------------reType=" + reType);
							var rowContent = "<tr id="
									+ rowNum
									+ ">"
									+ "<td style='text-align: center;'><input type='text'  id='proCode_"
									+ rowNum + "'  name='details[" + rowNum
									+ "].proCode' itemName='proCode' value='"
									+ data.proCode
									+ "' /><input type='hidden'  id='id_"
									+ rowNum + "'  name='details[" + rowNum
									+ "].id' itemName='id' value='" + data.id
									+ "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'>"
									+ "<input id='proId_" + rowNum
									+ "' type='hidden' name='details[" + rowNum
									+ "].proId' itemName='proId' value='"
									+ data.proId + "' />"
									+ "<input id='proName_" + rowNum
									+ "' type='text' name='details[" + rowNum
									+ "].proName' itemName='proName' value='"
									+ data.proName + "' /></td>";

							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='ruleCode_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleCode' itemName='ruleCode' value='"
									+ data.ruleCode + "'/><input id='ruleId_"
									+ rowNum + "' type='hidden' name='details["
									+ rowNum
									+ "].ruleId' itemName='ruleId' value='"
									+ data.ruleId + "'/><input id='ruleName_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].ruleName' itemName='ruleName' value='"
									+ data.ruleName + "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='enterPrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].enterPrice' readonly='readonly' itemName='enterPrice' value='"
									+ data.enterPrice + "'rowId='" + rowNum
									+ "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='salePrice_"
									+ rowNum
									+ "' type='text' name='details["
									+ rowNum
									+ "].salePrice' itemName='salePrice' value='"
									+ data.salePrice + "' rowId='" + rowNum
									+ "' /></td>";
							if (data.total == null || data.total == 'undefined') {
								data.total = 0;
							}
							if (data.summery == null
									|| data.summery == 'undefined') {
								data.summery = 0;
							}

							if (data.note == null || data.note == 'undefined') {
								data.note = "";
							}
							rowContent = rowContent
									+ "<td style='text-align: center;'><input  id='total_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].total' itemName='total' value='"
									+ data.total + "' rowId='" + rowNum
									+ "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='summery_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].summery' itemName='summery' value='"
									+ data.summery + "' /></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><input id='note_"
									+ rowNum + "' type='text' name='details["
									+ rowNum
									+ "].note' itemName='note' value='"
									+ data.note + "'  rowId='" + rowNum
									+ "'/></td>";
							rowContent = rowContent
									+ "<td style='text-align: center;'><button  class='btn btn-sm btn-danger' objId='"
									+ data.id
									+ "'  name='delItem' type='button'>删除</button></td>";
							rowContent = rowContent + "</tr>";
							itemTable.append(rowContent);
							rowNum++;

						}

						if (isView) {
							// readonly
							$("#stocksDialog input").attr("readonly",
									'readonly');
							$("#stocksDialog textarea").attr("readonly",
									'readonly');
							$("#stocksDialog select").attr("disabled",
									'disabled');
							$("#saveBtn").attr("disabled", 'disabled');
							$("#confirmBtn").attr("disabled", 'disabled');
							$("#itemsTable").attr("disabled", 'disabled');
							$("#giftItem").attr("disabled", 'disabled');
							$("#giftclearItem").attr("disabled", 'disabled');
							$("#proclearItem").attr("disabled", 'disabled');
							$("#giftAddItem").attr("disabled", 'disabled');
							$("#proAddItem").attr("disabled", 'disabled');
							$("#itemsTable button[name='delItem']").attr(
									"disabled", 'disabled');
							$("#giftItemTable button[name='delItem']").attr(
									"disabled", 'disabled');

						} else {
							$("#stocksDialog input").removeAttr("readonly");
							$("#stocksDialog textarea").removeAttr("readonly");
							$("#stocksDialog select").removeAttr('disabled');
							$("#saveBtn").removeAttr("disabled");
							$("#confirmBtn").removeAttr("disabled");
							$("#itemsTable").removeAttr("disabled");
							$("#giftItem").removeAttr("disabled");
							$("#giftclearItem").removeAttr("disabled");
							$("#proclearItem").removeAttr("disabled");
							$("#giftAddItem").removeAttr("disabled");
							$("#proAddItem").removeAttr("disabled");
							$("#itemsTable button[name='delItem']").removeAttr(
									"disabled");
							$("#giftItemTable button[name='delItem']")
									.removeAttr("disabled");

						}

					}

				});
